import os
import shutil
import os


# def print_relative_paths(base_folder):
#     """Print relative paths of all files in the given folder."""
#     for root, dirs, files in os.walk(base_folder):
#         for file in files:
#             # Get the relative path from the base_folder
#             relative_path = os.path.relpath(os.path.join(root, file), base_folder)
#             print(relative_path)
#
#
# print_relative_paths("/Users/tyrone/Documents/order/师傅维修资料/数据/pointlight")


def move_files_to_root_and_delete_empty_dirs(root_dir):
    # 遍历目录树
    for dirpath, dirnames, filenames in os.walk(root_dir, topdown=False):
        # 移动文件到根目录
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            new_file_path = os.path.join(root_dir, filename)

            # 如果文件在根目录已经存在，跳过或覆盖
            if os.path.exists(new_file_path):
                print(f"File {new_file_path} already exists, skipping.")
            else:
                shutil.move(file_path, new_file_path)
                print(f"Moved: {file_path} -> {new_file_path}")

        # 删除空文件夹
        if dirpath != root_dir:  # 确保不删除根目录
            try:
                os.rmdir(dirpath)
                print(f"Deleted empty folder: {dirpath}")
            except OSError:
                print(f"Directory not empty or could not be removed: {dirpath}")
# 指定要处理的目录
root_directory = '/Users/tyrone/Documents/images/'

# 调用函数
move_files_to_root_and_delete_empty_dirs(root_directory)
#
# # # 定义根目录
# root_dir = '/Users/tyrone/Documents/指示灯'
#
# # 遍历文件夹
# for brand_folder in os.listdir(root_dir):
#     brand_path = os.path.join(root_dir, brand_folder)
#
#     # 检查是不是文件夹
#     if os.path.isdir(brand_path):
#         for file_name in os.listdir(brand_path):
#             file_path = os.path.join(brand_path, file_name)
#
#             # 检查是不是文件
#             if os.path.isfile(file_path):
#                 # 提取前缀和编号，例如 扬子-扬子定频空调指示灯-0.png
#                 parts = file_name.split('-')
#                 if len(parts) >= 3:
#                     new_folder_name = parts[1]  # 扬子定频空调指示灯
#                     new_file_name = parts[-1]  # 0.png
#
#                     # 创建新的子文件夹路径
#                     new_folder_path = os.path.join(brand_path, new_folder_name)
#
#                     # 如果文件夹不存在，创建文件夹
#                     if not os.path.exists(new_folder_path):
#                         os.makedirs(new_folder_path)
#
#                     # 定义新文件路径
#                     new_file_path = os.path.join(new_folder_path, new_file_name)
#
#                     # 移动文件
#                     shutil.move(file_path, new_file_path)
#                     print(f"Moved {file_path} to {new_file_path}")


#
# import os
# import shutil
#
# def replace_special_chars(name):
#     """Replace `,` and `_` with `-` in the file/folder name."""
#     return name.replace(',', '-').replace('，', '-').replace('_', '-')
#
# def move_files_to_folder(src_folder, target_folder):
#     """Move all files from src_folder to target_folder."""
#     for item in os.listdir(src_folder):
#         s_item_path = os.path.join(src_folder, item)
#         t_item_path = os.path.join(target_folder, item)
#
#         # If it is a file, move it
#         if os.path.isfile(s_item_path):
#             shutil.move(s_item_path, t_item_path)
#         # If it is a folder, move its content recursively
#         elif os.path.isdir(s_item_path):
#             if not os.path.exists(t_item_path):
#                 shutil.move(s_item_path, t_item_path)
#             else:
#                 move_files_to_folder(s_item_path, t_item_path)
#
#     # After moving, if the folder is empty, delete it
#     if not os.listdir(src_folder):
#         os.rmdir(src_folder)
#
# def rename_folder_files(base_folder):
#     """Rename all files and folders inside the base_folder, and move files if needed."""
#     for root, dirs, files in os.walk(base_folder, topdown=False):
#         # Rename files
#         for name in files:
#             new_name = replace_special_chars(name)
#             old_path = os.path.join(root, name)
#             new_path = os.path.join(root, new_name)
#             if old_path != new_path:
#                 os.rename(old_path, new_path)
#
#         # Rename directories
#         for name in dirs:
#             old_dir_path = os.path.join(root, name)
#             new_name = replace_special_chars(name)
#             new_dir_path = os.path.join(root, new_name)
#             if old_dir_path != new_dir_path:
#                 if os.path.exists(new_dir_path):
#                     move_files_to_folder(old_dir_path, new_dir_path)
#                     if not os.listdir(old_dir_path):  # If old dir is empty after move
#                         os.rmdir(old_dir_path)
#                 else:
#                     os.rename(old_dir_path, new_dir_path)
#
# if __name__ == "__main__":
#     folder_path = '/Users/tyrone/Documents/images'
#     if os.path.exists(folder_path):
#         rename_folder_files(folder_path)
#         print(f"Renaming and moving completed for folder: {folder_path}")
#     else:
#         print("The specified folder does not exist.")

